//
//  ProductViewController.m
//  ShoppingExperience
//
//  Created by LiYinDi on 14-12-29.
//  Copyright (c) 2014年 RuiBen. All rights reserved.
//

#import "ProductViewController.h"

@interface ProductViewController ()
{
     NSUInteger previousSelectedIndex;
}
@end

@implementation ProductViewController
@synthesize productViewControllers = _productViewControllers;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}
-(void)viewWillAppear:(BOOL)animated
{
     _cellSelectedIndex=[Global getProductID];
//    if (_cellSelectedIndex<=9) {
//        [self setSelectedIndex:0];
//    }
//    else
//    {
         [self setSelectedIndex:_cellSelectedIndex];
//    }
    
    //  [Global setShopCartID:3];
    [Global setStyleDetailID:1];
   // [Global setHomeCustomID:3];
}
- (void)setSelectedIndex:(NSUInteger)selectedIndex
{
    if (selectedIndex<[self.productViewControllers count]) {
        if (previousSelectedIndex != NSNotFound)
        {
            UIViewController *previousViewController = [self.productViewControllers objectAtIndex:previousSelectedIndex];
            [previousViewController.view removeFromSuperview];
            [previousViewController removeFromParentViewController];
        }
        
        UIViewController *selectedViewController = [self.productViewControllers objectAtIndex:selectedIndex];
        [self addChildViewController:selectedViewController];
        selectedViewController.view.frame = CGRectMake(0, 0, DEFAULT_FirstLevelView_WIDTH, DEFAULT_FirstLevelView_HEIGHT);
        selectedViewController.view.autoresizingMask = UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleWidth;
        [self.view addSubview:selectedViewController.view];
        // remove previously selected view controller (if any)
        previousSelectedIndex=selectedIndex;
    }
    
}
-(void)productHomeBtnClick
{
    UIViewController *selectedViewController=[self.productViewControllers objectAtIndex:1];
    [self addChildViewController:selectedViewController];
    selectedViewController.view.frame= CGRectMake(0, 0, DEFAULT_FirstLevelView_WIDTH, DEFAULT_FirstLevelView_HEIGHT);
    
    [self.view addSubview:selectedViewController.view];
    
    UIViewController *previousViewController = [self.productViewControllers objectAtIndex:previousSelectedIndex];
    [previousViewController.view removeFromSuperview];
    [previousViewController removeFromParentViewController];
    previousSelectedIndex=1;

}
-(void)shopcartCashierFinish
{
    [self setSelectedIndex:4];
}
-(void)productSeriesBackClick
{
    [self setSelectedIndex:0];
}
-(void)productItemDetailBackBtnClick
{
    [self setSelectedIndex:1];
}
-(void)productSeriesCellClick
{
    UIViewController *selectedViewController=[self.productViewControllers objectAtIndex:2];
    [self addChildViewController:selectedViewController];
    selectedViewController.view.frame= CGRectMake(0, 0, DEFAULT_FirstLevelView_WIDTH, DEFAULT_FirstLevelView_HEIGHT);
    
    [self.view addSubview:selectedViewController.view];
    
    UIViewController *previousViewController = [self.productViewControllers objectAtIndex:previousSelectedIndex];
    [previousViewController.view removeFromSuperview];
    [previousViewController removeFromParentViewController];
    previousSelectedIndex=2;

}
-(void)seriesCellClick:(NSUInteger)index
{
    UIViewController *selectedViewController=[self.productViewControllers objectAtIndex:1];
    [self addChildViewController:selectedViewController];
    selectedViewController.view.frame= CGRectMake(0, 0, DEFAULT_FirstLevelView_WIDTH, DEFAULT_FirstLevelView_HEIGHT);
   
    [self.view addSubview:selectedViewController.view];
    
    UIViewController *previousViewController = [self.productViewControllers objectAtIndex:previousSelectedIndex];
    [previousViewController.view removeFromSuperview];
    [previousViewController removeFromParentViewController];
    previousSelectedIndex=1;
}
-(void)seriesDetailBackClick
{
    UIViewController *selectedViewController=[self.productViewControllers objectAtIndex:0];
    [self addChildViewController:selectedViewController];
    selectedViewController.view.frame= CGRectMake(0, 0, DEFAULT_FirstLevelView_WIDTH, DEFAULT_FirstLevelView_HEIGHT);
    
    [self.view addSubview:selectedViewController.view];
    
    UIViewController *previousViewController = [self.productViewControllers objectAtIndex:previousSelectedIndex];
    [previousViewController.view removeFromSuperview];
    [previousViewController removeFromParentViewController];
    previousSelectedIndex=0;
}
-(void)seriesDetailCellClick:(NSUInteger)index
{
    UIViewController *selectedViewController=[self.productViewControllers objectAtIndex:2];
    [self addChildViewController:selectedViewController];
    selectedViewController.view.frame= CGRectMake(0, 0, DEFAULT_FirstLevelView_WIDTH, DEFAULT_FirstLevelView_HEIGHT);
  
    [self.view addSubview:selectedViewController.view];
    
    UIViewController *previousViewController = [self.productViewControllers objectAtIndex:previousSelectedIndex];
    [previousViewController.view removeFromSuperview];
    [previousViewController removeFromParentViewController];
    previousSelectedIndex=2;

}
-(void)styleDetailBackClick
{
    UIViewController *selectedViewController=[self.productViewControllers objectAtIndex:1];
    [self addChildViewController:selectedViewController];
    selectedViewController.view.frame= CGRectMake(0, 0, DEFAULT_FirstLevelView_WIDTH, DEFAULT_FirstLevelView_HEIGHT);
    
    [self.view addSubview:selectedViewController.view];
    
    UIViewController *previousViewController = [self.productViewControllers objectAtIndex:previousSelectedIndex];
    [previousViewController.view removeFromSuperview];
    [previousViewController removeFromParentViewController];
    previousSelectedIndex=1;
}
-(void)addToShopCart
{
    UIViewController *selectedViewController=[self.productViewControllers objectAtIndex:3];
    [self addChildViewController:selectedViewController];
    selectedViewController.view.frame= CGRectMake(0, 0, DEFAULT_FirstLevelView_WIDTH, DEFAULT_FirstLevelView_HEIGHT);
    
    [self.view addSubview:selectedViewController.view];
    
    UIViewController *previousViewController = [self.productViewControllers objectAtIndex:previousSelectedIndex];
    [previousViewController.view removeFromSuperview];
    [previousViewController removeFromParentViewController];
    previousSelectedIndex=3;
}
-(void)ShopCartBtnClick
{
    UIViewController *selectedViewController=[self.productViewControllers objectAtIndex:3];
    [self addChildViewController:selectedViewController];
    selectedViewController.view.frame= CGRectMake(0, 0, DEFAULT_FirstLevelView_WIDTH, DEFAULT_FirstLevelView_HEIGHT);
    
    [self.view addSubview:selectedViewController.view];
    
    UIViewController *previousViewController = [self.productViewControllers objectAtIndex:previousSelectedIndex];
    [previousViewController.view removeFromSuperview];
    [previousViewController removeFromParentViewController];
    previousSelectedIndex=3;

}
-(void)detailShopCartBackClick
{
//    UIViewController *selectedViewController=[self.productViewControllers objectAtIndex:2];
//    [self addChildViewController:selectedViewController];
//    selectedViewController.view.frame= CGRectMake(0, 0, DEFAULT_FirstLevelView_WIDTH, DEFAULT_FirstLevelView_HEIGHT);
//    
//    [self.view addSubview:selectedViewController.view];
//    
//    UIViewController *previousViewController = [self.productViewControllers objectAtIndex:previousSelectedIndex];
//    [previousViewController.view removeFromSuperview];
//    [previousViewController removeFromParentViewController];
//    previousSelectedIndex=2;
   // if ([Global getShopCartID]==2) {
        [self setSelectedIndex:1];
   // }
   
}
-(void)productShopCartBackClick
{

    [self setSelectedIndex:2];

}
-(void)orderBackClick
{
    if ([Global getOrderID]==2) {
        [self setSelectedIndex:1];
    }
    else if ([Global getOrderID]==3)
    {
        [self setSelectedIndex:2];
    }

}
-(void)shopCartCashierBtnClick
{
    UIViewController *selectedViewController=[self.productViewControllers objectAtIndex:4];
    [self addChildViewController:selectedViewController];
    selectedViewController.view.frame= CGRectMake(0, 0, DEFAULT_FirstLevelView_WIDTH, DEFAULT_FirstLevelView_HEIGHT);
    
    [self.view addSubview:selectedViewController.view];
    
    UIViewController *previousViewController = [self.productViewControllers objectAtIndex:previousSelectedIndex];
    [previousViewController.view removeFromSuperview];
    [previousViewController removeFromParentViewController];
    previousSelectedIndex=4;
}
-(void)buyNowClick
{
    UIViewController *selectedViewController=[self.productViewControllers objectAtIndex:4];
    [self addChildViewController:selectedViewController];
    selectedViewController.view.frame= CGRectMake(0, 0, DEFAULT_FirstLevelView_WIDTH, DEFAULT_FirstLevelView_HEIGHT);
    
    [self.view addSubview:selectedViewController.view];
    
    UIViewController *previousViewController = [self.productViewControllers objectAtIndex:previousSelectedIndex];
    [previousViewController.view removeFromSuperview];
    [previousViewController removeFromParentViewController];
    previousSelectedIndex=4;

}
-(void)cashierBackBtnClick
{
    UIViewController *selectedViewController=[self.productViewControllers objectAtIndex:3];
    [self addChildViewController:selectedViewController];
    selectedViewController.view.frame= CGRectMake(0, 0, DEFAULT_FirstLevelView_WIDTH, DEFAULT_FirstLevelView_HEIGHT);
    
    [self.view addSubview:selectedViewController.view];
    
    UIViewController *previousViewController = [self.productViewControllers objectAtIndex:previousSelectedIndex];
    [previousViewController.view removeFromSuperview];
    [previousViewController removeFromParentViewController];
    previousSelectedIndex=3;
}
-(void)shopCartHomeBtnClick
{
    UIViewController *selectedViewController=[self.productViewControllers objectAtIndex:5];
    [self addChildViewController:selectedViewController];
    selectedViewController.view.frame= CGRectMake(0, 0, DEFAULT_FirstLevelView_WIDTH, DEFAULT_FirstLevelView_HEIGHT);
    
    [self.view addSubview:selectedViewController.view];
    
    UIViewController *previousViewController = [self.productViewControllers objectAtIndex:previousSelectedIndex];
    [previousViewController.view removeFromSuperview];
    [previousViewController removeFromParentViewController];
    previousSelectedIndex=5;

}
-(void)confirmDeliveryAddressBtnClick
{
    UIViewController *selectedViewController=[self.productViewControllers objectAtIndex:3];
    [self addChildViewController:selectedViewController];
    selectedViewController.view.frame= CGRectMake(0, 0, DEFAULT_FirstLevelView_WIDTH, DEFAULT_FirstLevelView_HEIGHT);
    
    [self.view addSubview:selectedViewController.view];
    
    UIViewController *previousViewController = [self.productViewControllers objectAtIndex:previousSelectedIndex];
    [previousViewController.view removeFromSuperview];
    [previousViewController removeFromParentViewController];
    previousSelectedIndex=3;

}
- (void)setProductViewControllers:(NSArray *)viewControllers
{
    _productViewControllers = [viewControllers copy];
    previousSelectedIndex=NSNotFound;
    // create tab bar items
}

- (void)viewDidLoad
{
    [super viewDidLoad];
  
    // Do any additional setup after loading the view from its nib.
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
